package com.flycms.module.activity.service;

import com.flycms.core.entity.PageVo;
import com.flycms.module.activity.dao.ActivityDao;
import com.flycms.module.activity.dao.ActivityDetailDao;
import com.flycms.module.activity.dao.ActivityTagDao;
import com.flycms.module.activity.enums.ActivityTypeEnum;
import com.flycms.module.activity.model.Activity;
import com.flycms.module.activity.model.ActivityDetail;
import com.flycms.module.activity.model.ActivityTag;
import com.flycms.module.activity.param.ActivityInfo;
import com.flycms.module.activity.param.ActivityQueryParam;
import com.flycms.utils.ConvertUtils;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Objects;

/**
 * @author mdoenglang
 */

@Service
public class ActivityService {

    @Autowired
    private ActivityDao activityDao;

    @Autowired
    private ActivityDetailDao activityDetailDao;

    @Autowired
    private ActivityTagDao activityTagDao;

    /**
     * 根据类型查找响应的活动数据
     *
     * @param param 查询参数
     * @return 对应活动数据
     */
    public PageVo<Activity> getActivityPage(ActivityQueryParam param) {
        //查询全部
        if (ActivityTypeEnum.CONSULT.getCode().equals(param.getType()) || Objects.isNull(param.getType())) {
            //统计数据
            int count = activityDao.count(param.getName());
            if (count <= 0) {
                return new PageVo<>(param.getPage());
            }
            //查询数据
            List<Activity> activities = activityDao.selectByPage(param.getName(),
                            (param.getPage() - 1) * param.getPageSize(), param.getPageSize());
            return new PageVo<>(param.getPage(), count, activities);
        }
        //统计数据
        int count = activityTagDao.countByType(param.getName(),param.getType());
        if (count <= 0) {
            return new PageVo<>(param.getPage());
        }
        //查询数据
        List<Long> ids = activityTagDao.selectIdByType(param.getName(), param.getType(),
                (param.getPage() - 1) * param.getPageSize(), param.getPageSize());
        List<Activity> activities = activityDao.selectByIds(ids);
        return new PageVo<>(param.getPage(), count, activities);
    }


    /**
     * 根据活动ID 获取活动详情
     *
     * @param id 活动ID
     * @return 返回结果
     */
    public ActivityDetail getDetailByActivityId(Long id) {
        Activity activity = activityDao.selectById(id);
        if (Objects.isNull(activity)) {
            return null;
        }
        return activityDetailDao.selectByActivityId(activity.getId());
    }

    /**
     * 获取最新三个活动
     *
     * @return 返回最新三个活动
     */
    public List<Activity> getNewActivityList() {
        return activityDao.selectByPage(null, 0, 3);
    }

    /**
     * 获取小于当前的ID
     *
     * @param id 当前ID
     * @return 小于当前的ID
     */
    public Activity getPreActivity(Long id) {
        return activityDao.getPreActivity(id);
    }

    /**
     * 获取大于当前的ID
     *
     * @param id 当前ID
     * @return 大于当前的ID
     */
    public Activity getNextActivity(Long id) {
        return activityDao.getNextActivity(id);
    }

    /**
     * 保存活动
     *
     * @param param 活动参数
     */
    @Transactional(rollbackOn = Exception.class)
    public void save(ActivityInfo param) {
        Activity activity = ConvertUtils.sourceToTarget(param, Activity.class);
        activityDao.insertSelective(activity);
        if (Objects.nonNull(param.getDetail())) {
            ActivityDetail detail = param.getDetail();
            detail.setActivityId(activity.getId());
            activityDetailDao.insertSelective(detail);
        }
        if (CollectionUtils.isNotEmpty(param.getTags())) {
            List<ActivityTag> tags = param.getTags();
            tags.forEach(t -> t.setActivityId(activity.getId()));
            activityTagDao.batchInsert(tags);
        }
    }

    /**
     * 更新活动
     *
     * @param param 活动参数
     */
    @Transactional(rollbackOn = Exception.class)
    public void update(ActivityInfo param) {
        Activity activity = ConvertUtils.sourceToTarget(param, Activity.class);
        activityDao.update(activity);
        if (Objects.nonNull(param.getDetail())) {
            ActivityDetail detail = param.getDetail();
            activityDetailDao.update(detail);
        }
        if (CollectionUtils.isNotEmpty(param.getTags())) {
            activityTagDao.del(param.getId());
            activityTagDao.batchInsert(param.getTags());
        }
    }

    /**
     * 删除活动
     *
     * @param id 活动ID
     */
    @Transactional(rollbackOn = Exception.class)
    public void del(Long id) {
        activityDao.delById(id);
        activityDetailDao.delByActivityId(id);
        activityTagDao.delByActivityId(id);
    }

    /**
     * 获取活动标签
     * @param ids 活动ID
     */
    public List<ActivityTag>  getTagsByActivityIds(List<Long> ids) {
        if(CollectionUtils.isEmpty(ids)){
            return Lists.newArrayList();
        }
        return activityTagDao.getTagsByActivityIds(ids);
    }

    public Activity getActivityById(Long id) {
        return activityDao.selectById(id);
    }
}
